import request from '../utils/request'

/**
 * 用户相关API接口
 */

export interface User {
  id: string
  username: string
  name: string
  avatar: string
  email?: string
  phone?: string
  role: string
  status: 'active' | 'inactive' | 'locked'
  permissions?: string[]
  createTime: string
  lastLoginTime?: string
  department?: string
}

export interface UserListParams {
  keyword?: string
  role?: string
  status?: string
  page?: number
  pageSize?: number
}

export interface UserFormData {
  id?: string
  username: string
  name: string
  password?: string
  email?: string
  phone?: string
  role: string
  status: 'active' | 'inactive'
  permissions?: string[]
  department?: string
}

/**
 * 获取用户列表
 */
export function getUsers(params: UserListParams = {}) {
  return request<User[]>({
    url: '/users',
    method: 'GET',
    params
  })
}

/**
 * 获取用户详情
 */
export function getUserDetail(id: string) {
  return request<User>({
    url: `/users/${id}`,
    method: 'GET'
  })
}

/**
 * 创建用户
 */
export function createUser(data: UserFormData) {
  return request<User>({
    url: '/users',
    method: 'POST',
    data
  })
}

/**
 * 更新用户
 */
export function updateUser(id: string, data: Partial<UserFormData>) {
  return request<User>({
    url: `/users/${id}`,
    method: 'PUT',
    data
  })
}

/**
 * 删除用户
 */
export function deleteUser(id: string) {
  return request<null>({
    url: `/users/${id}`,
    method: 'DELETE'
  })
}

/**
 * 更新用户状态
 */
export function updateUserStatus(id: string, status: 'active' | 'inactive' | 'locked') {
  return request<User>({
    url: `/users/${id}/status`,
    method: 'PATCH',
    data: { status }
  })
}

/**
 * 重置用户密码
 */
export function resetUserPassword(id: string) {
  return request<{ password: string }>({
    url: `/users/${id}/reset-password`,
    method: 'POST'
  })
}

/**
 * 获取角色列表
 */
export function getRoles() {
  return request<Array<{value: string, label: string}>>({
    url: '/roles',
    method: 'GET'
  })
} 